package dto;

// Generated May 6, 2011 4:19:20 PM by Hibernate Tools 3.3.0.GA

import SessionFactory.SessionFactoryUtil;
import java.util.Iterator;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Readmark.
 * @see dto.Readmark
 * @author Hibernate Tools
 */
public class ReadmarkHome {

	private static final Log log = LogFactory.getLog(ReadmarkHome.class);

	private final SessionFactory sessionFactory = SessionFactoryUtil.getInstance();

	public void persist(Readmark transientInstance) {
		log.debug("persisting Readmark instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Readmark instance) {
		log.debug("attaching dirty Readmark instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Readmark instance) {
		log.debug("attaching clean Readmark instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Readmark persistentInstance) {
		log.debug("deleting Readmark instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Readmark merge(Readmark detachedInstance) {
		log.debug("merging Readmark instance");
		try {
			Readmark result = (Readmark) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Readmark findById(Integer id) {
		log.debug("getting Readmark instance with id: " + id);
		try {
			Readmark instance = (Readmark) sessionFactory.getCurrentSession()
					.get("dto.Readmark", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Readmark> findByExample(Readmark instance) {
		log.debug("finding Readmark instance by example");
		try {
			List<Readmark> results = (List<Readmark>) sessionFactory
					.getCurrentSession().createCriteria("dto.Readmark").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
        public boolean getMarks(Topics topic) {

//        SessionFactoryUtil.startNew();
//        System.out.println("inside getmarks method");

        Readmark mark     = new Readmark();
        String   query    = "from Readmark marks where marks.topics.topicid like :topic";
        Query    hqlQuery = sessionFactory.getCurrentSession().createQuery(query).setInteger("topic", topic.getTopicid());
        List     myResult = hqlQuery.list();
        Iterator it       = myResult.iterator();

        while (it.hasNext()) {
            mark = (Readmark) it.next();

            // System.out.println(mark.getNumposts());
            Integer numPosts = mark.getNumposts();
            Integer num      = new Integer(1);

//            System.out.println("nuuuuuuuuuuum1111=" + num);
            num = numPosts + num ;
//            System.out.println("nummmposts=" + numPosts);
//            System.out.println("nuuuuuuuuuuum=" + num);
            mark.setNumposts(num);
            attachDirty(mark);
        }

        return true;
    }

    // ===============================Amir===========================
 public boolean readmark(Topics topic, JfMembers member) {
        boolean   flag     = false;
        Readmark  readmark = new Readmark();
        
        readmark.setJfMembers(member);
        readmark.setTopics(topic);
        readmark.setNumposts(topic.getPostses().size());

        try {
//            session.beginTransaction();
            sessionFactory.getCurrentSession().persist(readmark);
//            session.getTransaction().commit();
            flag = true;
        } catch (Exception e) {
            flag = false;
        }

        return flag;
    }
    public boolean getReadMarks(Topics topic, JfMembers member)
    {
        boolean flag=false;
         Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from Readmark");
        List result = hqlQuery.list();
        Iterator next = result.iterator();
   
        while (next.hasNext())
        {    
             Readmark read=new Readmark();
             read=(Readmark)next.next();
            System.out.println("/////////////////////////////////// test mark:"+read.getTopics().getTopicid());

            if ((read.getTopics().getTopicid().equals(topic.getTopicid()))&&(read.getJfMembers().getMemberId().equals(member.getMemberId())))
            {
                
                flag=true;
                  
            }

        }
          
              return flag;
    }
}
